import javax.sound.midi.Sequence;

public class Producer_legacy_dwiteeya implements Runnable {

	Thread thread;
	int currentNote, targetNote;
	int currentDuration, targetDuration;
	Sequence sequence;
	boolean produce_more;
	
	
	Raga raga = new Raga("/media/Acad/FinalMajor/Oryx/src/shaymkalyan.xml");
	public Producer_legacy_dwiteeya() {
		
		
		
	
		currentNote= 60;
		targetNote = 60;
		thread = new Thread(this, "PRODUCER_THREAD");
		System.out.println("New thread: " + thread);
		produce_more= false;

	}

	public void startThread() {
		thread.start();// Start the thread
		System.out.println("started thread: " + thread);

		produce_more = true;

	}

	
	
	// Dummy function to generate a series of notes.
	public int getNextNote() {
		//targetNote= Global.gui.getNoteValue();
		targetNote=Global.freehand.getTargetNote();
		
		
		
		if (currentNote > targetNote) {
			currentNote = currentNote  - 1;
		} else if (currentNote < targetNote) {
			currentNote = currentNote + 1;
		}
		return currentNote;
	}
	public int getNextDuration()
	{
		targetDuration= Global.freehand.getTargetDuration();

		if (currentDuration > targetDuration) {
			currentDuration = currentDuration - 100;
		} else if (currentDuration <= targetDuration) {
			currentDuration = currentDuration + 100;
		}
		return currentDuration;
	}

	
	@Override
	public synchronized void run() {
		
		int temp_note;
		int temp_duration;
		int count=0;
		while(this.produce_more!=false)
		{
	
			if(Global.musicBuffer.list.size()<1)
			{
			System.out.println("in run of producer");
			temp_note = getNextNote();
			temp_duration= getNextDuration();
			Global.musicBuffer.pushEvent(1, temp_note, 126, 0,temp_duration);
			count++;
			}
		} 
		
		Global.musicBuffer.pushEvent(-1, 0, 0, 0,0);
		System.out.println("out of producer run");

	}

}
